package chapter2;


import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import static java.util.Comparator.comparing;

public class AppleTest {
    public static <T> List<T> filter(List<T> list,Predicate<T> p){
        List<T> result = new ArrayList<>();
        for(T e:list){
            if(p.test(e)){
                result.add(e);
            }
        }
        return result;
    }

    @Test
    public void test() {
    /*    List<Apple> apples = new ArrayList<>();
        AppleGreenColorPredicate appleGreenColorPredicate = new AppleGreenColorPredicate();
        apples.add(new Apple(300,"green"));
        apples.add(new Apple(300,"Black"));
        List<Apple>apples1 = Apple.filterApples(apples,appleGreenColorPredicate);
        for(Apple apple:apples1){
            System.out.println(apple);
        }*/

    /*
        List<Apple> apples = new ArrayList<>();
        apples.add(new Apple(300,"green"));
        apples.add(new Apple(300,"Black"));

        List<Apple> greenApples = filter(apples,(Apple apple)->"green".equals(apple.getColour()));
        for (Apple apple:greenApples)
         {
             System.out.println(apple);
         }
        Supplier<Apple> c1 = Apple::new;
        Apple a1 = c1.get();

        Function<Integer,Apple> c2 = Apple::new;
        Apple a2 = c2.apply(110);*/

        List<Integer> weights = Arrays.asList(7,3,4,10);
        List<Apple> apples = map(weights,Apple::new);
        apples.sort(comparing(Apple::getWeight));
        apples.sort(comparing(Apple::getWeight).reversed());
        apples.sort(comparing(Apple::getWeight).reversed().thenComparing(Apple::getColour));
        for(Apple apple:apples){
            System.out.println(apple);
        }

        BiFunction<Integer,String,Apple> c3 = Apple::new;
        Apple a3 = c3.apply(110,"green");


    }





     public static List<Apple> map(List<Integer> lists,
                                   Function<Integer,Apple> f){
        List<Apple> result =  new ArrayList<>();
        for(Integer e:lists){
            result.add(f.apply(e));
        }
        return result;
     }
}
